package xushuo.dao.entity.weixin.managementUser;

import net.sf.json.JSONObject;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.rmi.runtime.Log;
import xushuo.dao.entity.weixin.user.*;

import java.io.IOException;

/**
 * Create by zhangpe0312@qq.com on 2018/2/5.
 * <p>
 * 将服务器传过来的Json转换成相应的对象进行保存
 *
 * @see ReqToPublicUserInfo
 */
@Service
public class JsonToObject {

    private static Logger logger = Logger.getLogger(JsonToObject.class);

    @Autowired
    ReqToPublicUserInfo reqToPublicUserInfo;

    private GetInfo JsonToChooseAddressInfo(String str) {

        logger.info("打印ChooseAddressInfo str " + str);

        if (str.equals("")) {
            return null;
        }

        ChooseAddressInfo info = null;
        try {
            JSONObject jsonObject = JSONObject.fromObject(str);
            String errMsg = (String) jsonObject.get("errMsg");
            String userName = (String) jsonObject.get("userName");
            String postalCode = (String) jsonObject.get("postalCode");
            String provinceName = (String) jsonObject.get("provinceName");
            String cityName = (String) jsonObject.get("cityName");
            String countyName = (String) jsonObject.get("countyName");
            String detailInfo = (String) jsonObject.get("detailInfo");
            String nationalCode = (String) jsonObject.get("nationalCode");
            String telNumber = (String) jsonObject.get("telNumber");

            info = new ChooseAddressInfo.Builder()
                    .setDetailInfo(detailInfo)
                    .setErrMsg(errMsg)
                    .setCityName(cityName)
                    .setCountyName(countyName)
                    .setNationalCode(nationalCode)
                    .setPostalCode(postalCode)
                    .setTelNumber(telNumber)
                    .setProvinceName(provinceName)
                    .setUserName(userName)
                    .setGetTime()
                    .keep();
        } catch (Exception e) {
            logger.error("添加用户信息失败 " + e);
        }

        return info;
    }

    private GetInfo JsonToGetPhoneNum(String str) {

        JSONObject jsonObject = JSONObject.fromObject(str);
        String phoneNumber = (String) jsonObject.get("phoneNumber");
        String purePhoneNumber = (String) jsonObject.get("purePhoneNumber");
        String countryCode = (String) jsonObject.get("countryCode");

        GetPhoneNum getPhoneNum = new GetPhoneNum(phoneNumber, purePhoneNumber, countryCode);

        return getPhoneNum;
    }

    private GetInfo JsonToUserInfo(String str) {

        logger.info("UserInfo str " + str);

        JSONObject jsonObject = JSONObject.fromObject(str);
        String nickName = (String) jsonObject.get("nickName");
        String avatarUrl = (String) jsonObject.get("avatarUrl");
        int gender = (int) jsonObject.get("gender");
        String city = (String) jsonObject.get("city");
        String province = (String) jsonObject.get("province");
        String country = (String) jsonObject.get("country");
        String language = (String) jsonObject.get("language");

        GetUserInfo userInfo = new GetUserInfo.Builder()
                .setNickName(nickName)
                .setAvatarUrl(avatarUrl)
                .setGender(gender)
                .setCity(city)
                .setProvince(province)
                .setCountry(country)
                .setLanguage(language)
                .setGetTime()
                .keep();

        return userInfo;
    }

    private GetInfo JsonToPublicUserInfo(String openid) throws IOException {
        return reqToPublicUserInfo.getUserInfor(openid);
    }

    private GetInfo JsonToUserLoginInfo(String str) {


        logger.info("UserLoginInfo str " + str);

        JSONObject jsonObject = JSONObject.fromObject(str);
        String session_key = jsonObject.getString("session_key");
        String openid = jsonObject.getString("openid");

        UserLoginInfo loginInfo = new UserLoginInfo.Builder()
                .setOpenid(openid)
                .setSession_key(session_key)
                .setGetTime()
                .keep();
        return loginInfo;
    }

    /**
     * 这个方法为这个类的入口，可以自动判断这个字符串是属于哪个
     * 实体的
     *
     * @param str 微信小程序传过来的json字符串
     * @return 一个对应的实体
     */
    public GetInfo jsonToObject(String str) throws Exception {

        logger.info("" + str);

        if (str.equals("") || str.length() == 0 || str == ""){
            throw new NullPointerException(str + " length :" + str.length());
        }

        //JsonToChooseAddressInfo
        if (str.contains("telNumber")) {
            return JsonToChooseAddressInfo(str);
        }

        //JsonToGetPhoneNum
        if (str.contains("phoneNumber")) {
            return JsonToGetPhoneNum(str);
        }

        //JsonToUserInfo
        if (str.contains("gender")) {
            return JsonToUserInfo(str);
        }

        //JsonToPublicUserInfo
        if (str.contains("sex")) {
            return JsonToPublicUserInfo(str);
        }

        //JsonToUserLoginInfo
        if (str.contains("session_key")) {
            return JsonToUserLoginInfo(str);
        }

        throw new Exception("不知道 " + str + " 是什么字符串");
    }
}
